import {_decorator, Component, director, Label, Node, ProgressBar,Prefab,resources} from 'cc';
import {LevelCon} from '../core/controller/LevelCon';
import {logUtil} from "../framework/util/logUtil";
import {Constant} from "../config/Constant";
import {UiManager} from '../framework/manager/UiManager';

const {ccclass, requireComponent, property} = _decorator;

@ccclass('UIGame')
@requireComponent(LevelCon)
export class UIGame extends Component {
    //日志控制器
    log = logUtil.instance
    logLevel = "UIGame"

    //终点
    @property(Node)
    UIResourceNode: Node | null = null;

    UILevelInfo

    //经验条长度
    private test_expBar: number = Constant.GAME_SET.ExpBar;

    //按钮预制体
    @property(Prefab)
    HpHitNumPrefab: Prefab | null = null;

    onLoad() {
        //动态加载预制体
        resources.load(Constant.DYNAMIC_RESOURCE.Ui.HpHitNumPath,Prefab,
            (err, prefab) => {
                if (err) {
                    this.log.log1(this.logLevel,Constant.LOG_CONTENT_LEVEL,this.log.LOG_LEVEL.ERROR,`伤害数值字体加载`, err)
                    return;
                }
                this.HpHitNumPrefab = prefab
            });

        this.UILevelInfo=director.getScene().getChildByPath(Constant.STATIC_NODE_PATH.UILevelInfo)
        //初始化界面数据
        this.initFrame()

        this.node.on(Constant.EVENT_NAME.onResourceUpdate, this.onResourceUpdate, this)

        this.initLevelInfo()

        this.node.on(Constant.EVENT_NAME.initLevelInfo, this.initLevelInfo, this)

        this.node.on(Constant.EVENT_NAME.onExpUpdate, this.onExpUpdate, this)
        this.node.on(Constant.EVENT_NAME.onBarrierUpdate, this.onBarrierUpdate, this)
    }

    onDestroy() {
        this.node.off(Constant.EVENT_NAME.onResourceUpdate, this.onResourceUpdate, this);
    }

    // UIBranchChoose
    /*`*
     * 选择兵种
     * @param {Event} event UISkillBtn下btn节点
     * @param {string} customEventData 传输数据
     */
    onBranchChoose(event: Event, customEventData: string) {
        this.log.log(this.logLevel, `点击了${customEventData}`)
        this.node.emit(Constant.EVENT_NAME.onBranchCreate, customEventData)
    }

    initFrame() {
        this.UIResourceNode.children[0].children[0].getComponent(Label).string = LevelCon.instance.GSpawn + ""
        this.UIResourceNode.children[1].children[0].getComponent(Label).string = LevelCon.instance.MSpawn + ""
        this.UIResourceNode.children[2].children[0].getComponent(Label).string = LevelCon.instance.WSpawn + ""
    }

    onResourceUpdate(type: number, num: number, reduce: boolean) {
        const spawnKey = type == 1 ? 'MSpawn' : type == 2 ? 'WSpawn' : 'GSpawn';
        const operation = reduce ? (x, y) => x - y : (x, y) => x + y;

        LevelCon.instance[spawnKey] = operation(LevelCon.instance[spawnKey], num);
        this.UIResourceNode.children[type].children[0].getComponent(Label).string = LevelCon.instance[spawnKey] + "";
    }

    onBuffShow(){
        if (director.isPaused()) {
            UiManager.instance.hideDialog(Constant.DIALOG_DEF.UIBuff);
        } else {
            UiManager.instance.showDialog(Constant.DIALOG_DEF.UIBuff);
        }
    }

    onPauseGame() {
        if (director.isPaused()) {
            director.resume();
        } else {
            director.pause();
        }
    }

    onRestartGame(){
        //ui界面修正
        this.node.getComponent(LevelCon).restartGame()

        this.initFrame()
    }

    level = 1; // 假设当前等级是 1

    // 创建一个函数来生成等级文本
    getLevelText(currentLevel) {
        return `等级${currentLevel}`;
    }

    initLevelInfo(){
        this.level=1
        this.UILevelInfo.children[0].getComponent(Label).string=this.getLevelText(1)
        this.UILevelInfo.children[1].getComponent(ProgressBar).progress=0
        this.UILevelInfo.children[3].getComponent(Label).string=1
    }

    onLevelUpdate(num?:number) {
        this.level=num?num:this.level += 1;
        this.UILevelInfo.children[0].getComponent(Label).string=this.getLevelText(this.level)

        this.onBuffShow()
    }

    onExpUpdate(progress: number) {
        let oldPro=this.UILevelInfo.children[1].getComponent(ProgressBar).progress
        let tempPro=this.test_expBar*oldPro+progress
        if(tempPro>this.test_expBar){
            this.UILevelInfo.children[1].getComponent(ProgressBar).progress=(tempPro-this.test_expBar)/this.test_expBar
            this.onLevelUpdate()
        }else{
            this.UILevelInfo.children[1].getComponent(ProgressBar).progress=tempPro/this.test_expBar
        }

    }

    onBarrierUpdate(count:number) {
        this.UILevelInfo.children[3].getComponent(Label).string=count
    }
}


